<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;
use App\AccountLog;
use app\common\model\AppAccountLog;
use app\common\model\ApplicationOrder;
use app\common\model\ThirdApplicationResultLog;

use think\Db;



/**
 * 倍投控制器
 */
class FakeData extends AdminBase
{

    public function FakeDataList()
    {
        $where = $this->logicFakeData->getWhere($this->param);
        $this->assign('list', $this->logicFakeData->getFakeDataList($where, 'a.*', 'a.create_time asc'));
        return $this->fetch('fakedata_list');
    }

    public function FakeDataAdd()
    {
        $this->FakeDataCommon();
        return $this->fetch('fakedata_edit');
    }

    public function FakeDataEdit()
    {
        $this->FakeDataCommon();
        $info = $this->logicFakeData->getFakeDataInfo(['a.id' => $this->param['id']], 'a.*');
        $this->assign('info', $info);
        // $this->assign('product',Db::name("gambler_king_investment")->where("1=1")->select());
        return $this->fetch('fakedata_edit');
    }


    public function FakeDataCommon()
    {
        $this->assign("king",Db::name("gambler_king")->where("1=1")->select());
        $this->assign('product',Db::name("gambler_king_investment")->where("1=1")->select());
        IS_POST && $this->jump($this->logicFakeData->FakeDataEdit($_POST));
    }


    public function FakeDataSetStatus(){
        $id = $this->param['id'];
        $status = $this->param['status'];
        $res = Db::name('fakedata')->where(['id'=>$id])->update(['status'=>$status]);
        if(!$res){
            $this->error('修改失败');
        }else{
            $this->success('修改成功');
        }
    }

    public function FakeDataAdd2(){
        $templates = Db::name("fake_data_template")->where("1=1")->select();
        $this->assign("templates",$templates);
        return $this->fetch("fakedata_add2");
    }

    public function FakeDataAddTemplate(){
        $this->assign('product',Db::name("gambler_king_investment")->where("1=1")->select());
        if(IS_POST){
            $arr = array();
            foreach($this->param["king"] as $k=>$v){
                $data = [];
                $data["king"] = $v;
                $data["product_id"]=$this->param["product_id"][$k];
                $data["percent"]=$this->param["percent"][$k];
                $data["money"]=$this->param["money"][$k];
                $data["win_count"]=$this->param["win_count"][$k];
                $data["loss_count"]=$this->param["loss_count"][$k];
                $data["draw_count"]=$this->param["draw_count"][$k];
                foreach(explode(',',$this->param["user_name"][$k]) as $key=>$value){
                    $data['user_name'] = $value;
                    $arr[]=$data;
                }
            };
            
            Db::startTrans();
            try{
                Db::name("fake_data")->insertAll($arr);
                Db::commit();
            }catch(\Exception $e){
                Db::rollback();
                $this->error("添加失败");
            }
            $this->success("添加成功");
        }else{
            $template_id = $this->param["template_id"];
            $data = Db::name("fake_data_template")->where(["id"=>$template_id])->find();
            $arr = json_decode($data["data"],true);
            $this->assign("data",$arr);
            return $this->fetch("fakedata_add_template");
        }
    }

    public function FakeDataTemplateAdd(){
        $this->assign('product',Db::name("gambler_king_investment")->where("1=1")->select());
        if(IS_POST){
            $name = $this->param['template_name'];
            $arr = array();
            foreach ($this->param['king'] as $k=>$v){
                $arr[$k]['king']=$v;
                $arr[$k]["product_id"]=$this->param["product_id"][$k];
                $arr[$k]["user_name"]=$this->param["user_name"][$k];
                $arr[$k]["percent"]=$this->param["percent"][$k];
                $arr[$k]["money"]=$this->param["money"][$k];
                $arr[$k]["win_count"]=$this->param["win_count"][$k];
                $arr[$k]["loss_count"]=$this->param["loss_count"][$k];
                $arr[$k]["draw_count"]=$this->param["draw_count"][$k];
            }
            $json_data = json_encode($arr);
            $data = array(
                "name"=>$name,
                "data"=>$json_data,
                "status"=>1,
                "create_time"=>time()
            );
            Db::startTrans();
            try{
                Db::name("fake_data_template")->insert($data);
                Db::commit();
            }catch(\Exception $e){
                Db::rollback();
                $this->error("添加失败".$e->getMessage());
            }
            $this->success("添加成功");
        }else{
            $data = $this->param['data'];
            $list = Db::name("fake_data")->where(["id"=>["in",$data]])->select();

            $this->assign('list',$list);
            return $this->fetch('fakedata_template');
        }

    }




}
